﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SmartSiteManagementSystem.AdminWeb.Extensions;
using SmartSiteManagementSystem.AdminWeb.Filter;
using SmartSiteManagementSystem.AdminWeb.Models;
using SmartSiteManagementSystem.IServices;
using SmartSiteManagementSystem.Models.Admin;

namespace SmartSiteManagementSystem.AdminWeb.Controllers
{

  
    public class HomeController : AdminBaseControllers
    {

        private readonly ILogger<HomeController> _logger;
        //注入后台服务
        private readonly IAdminService _adminService;
        public HomeController( IAdminService adminService, ILogger<HomeController> logger) : base(adminService)
        {
            _logger = logger;
           _adminService = adminService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [SkipFilterAttribute]
        public IActionResult Index()
        {
            #region 验证是否已登录

            AdminUserInfo AdminObj = HttpContext.Session.Get<AdminUserInfo>("SSMS_AdminUserObj");
            if (AdminObj == null)
            {
                return RedirectToAction("Index", "Login");
            }

            #endregion 验证是否已登录

            //加载样式
            ViewBag.BodyClass = "easyui-layout";
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Info()
        {
            return View();
        }



        /// <summary>
        /// 获取左侧菜单数据
        /// </summary>
        [SkipFilterAttribute]
        public void GetLeft()
        {
            AdminUserInfo AdminObj = HttpContext.Session.Get<AdminUserInfo>("SSMS_AdminUserObj");
            if (AdminObj != null)
            {
                IList<AdminMenuInfo> MenuList = _adminService.GetNavigationMenu(AdminObj.AdminUserId, AdminObj.UserType);
                HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(_adminService.GetTree(MenuList)));
            }
        }



        /// <summary>
        /// 没有权限提示页
        /// </summary>
        /// <returns></returns>
        [SkipFilterAttribute]
        public ActionResult noRight()
        {
            return View();
        }

        [SkipFilterAttribute]
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
